<%@ page import="java.io.*, java.text.*, java.util.Date, java.sql.*, javax.servlet.*, javax.servlet.http.*, java.util.*" %>
<%
String usernameSC = (String) session.getAttribute("session_name");
String idSC = (String) session.getAttribute("id");
String typeSC = (String) session.getAttribute("type");
if(usernameSC == null || !(typeSC.equals("committee")))
	response.sendRedirect("error.jsp");
	
	
String driver ="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/projects";
String username = "ia";
String password = "123";

Enumeration variables = request.getParameterNames();

ArrayList names = new ArrayList();
ArrayList sts = new ArrayList();
for(int i=0;variables.hasMoreElements();i++)
{
	String name = (String)variables.nextElement();
	if(!name.substring(0,3).equals("pro"))
	{
		names.add(name.substring(2,name.length()));
		sts.add(request.getParameterValues(name));
	}
}
for(int i=0;i<sts.size();i++)
{
	String [] va = (String[])sts.get(i);
	for(int j=0;j<va.length;j++)
		out.println(va[j] + "<br/>");
}


try
{
	Class.forName(driver).newInstance();
	
	Connection connection = DriverManager.getConnection(url, username, password);
	connection.createStatement().executeUpdate("delete from join_in");
	for(int i=0;i<names.size();i++)
	{
		String [] stus = (String[])sts.get(i);
		for(int j=0;j<stus.length;j++)
		{
			String query = "insert into join_in (st_id,proj_id) values ('"+stus[j]+"','"+(String)names.get(i)+"')";
			String query2 = "update student set proj_id = "+(String)names.get(i)+" where id = "+ stus[j];
			out.println(query+"<br/>");
			connection.createStatement().executeUpdate(query);
			connection.createStatement().executeUpdate(query2);
		}
	}
	
	connection.close();
	
	response.sendRedirect("assignProjectToStudents.jsp");
}catch(ClassNotFoundException cnfe)
{
		out.println("<br>Couldn't find class file" + cnfe); 
}catch(SQLException sqle)
{
		out.println("<br>SQL Exception: " + sqle);
}catch(InstantiationException ie)
{
		out.println("<br>Instant exception : " + ie);
}catch(IllegalAccessException iae)
{
		out.println("<br>Illegal access exception : " + iae);
}

%>